package com.guangfeng.yql.comm.netty.verify;

import com.guangfeng.yql.comm.netty.enumerate.NettyMessageType;
import com.guangfeng.yql.comm.netty.util.NettyMessageUtil;
import com.guangfeng.yql.comm.netty.util.NettyResponseBeanUtil;
import com.guangfeng.yql.comm.netty.util.TextWebSocketFrameFormatUtil;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import org.springframework.stereotype.Component;

@Component
public class NettyMessageVerify implements DataVerify {


    @Override
    public boolean verify(ChannelHandlerContext ctx, Object msg) {
        try {
            TextWebSocketFrame textWebSocketFrame = (TextWebSocketFrame) msg;
            NettyMessageType type = NettyMessageType.getEnum(Integer.valueOf(TextWebSocketFrameFormatUtil.get(textWebSocketFrame, "code")));
            if (type == null) {
                throw new RuntimeException("");
            }
        } catch (Throwable e) {
            ctx.channel().writeAndFlush(NettyMessageUtil.textMessage(NettyResponseBeanUtil.parameterError("参数异常")));
            return false;
        }
        return true;
    }
}
